<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  
  
  <link rel="shortcut icon" href="img/favicon.ico">
  <title>IOFile - Java Note</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700" />

  <link rel="stylesheet" href="css/theme.css" />
  <link rel="stylesheet" href="css/theme_extra.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css" />
  
  <script>
    // Current page data
    var mkdocs_page_name = "IOFile";
    var mkdocs_page_input_path = "IO.md";
    var mkdocs_page_url = null;
  </script>
  
  <script src="js/jquery-2.1.1.min.js" defer></script>
  <script src="js/modernizr-2.8.3.min.js" defer></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
    <div class="wy-side-scroll">
      <div class="wy-side-nav-search">
        <a href="." class="icon icon-home"> Java Note</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="./search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="index.html">Home</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="StringX.html">StringX</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Collection.html">Collection</a>
                    </li>
                </ul>
                <ul class="current">
                    <li class="toctree-l1 current"><a class="reference internal current" href="IO.html">IOFile</a>
    <ul class="current">
    <li class="toctree-l2"><a class="reference internal" href="#random-access-file">Random-Access File</a>
    </li>
    </ul>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Lambda.html">Lambda</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Generic.html">Generic</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Stream.html">Stream</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Atomic.html">Atomic</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Reflect.html">Reflect</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="JavaFX.html">JavaFX</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Sundry.html">Sundry</a>
                    </li>
                </ul>
      </div>
    </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href=".">Java Note</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href=".">Docs</a> &raquo;</li>
    
      
    
    <li>IOFile</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
  </ul>
  
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <p><img alt="JavaIOInherit" src="IO/photo/JavaIOInherit.jpg" /></p>
<h2 id="file">File</h2>
<blockquote>
<p><code>File</code>  class will deal a String as the true file </p>
</blockquote>
<p>for example</p>
<pre><code class="language-java">public void makeD(String dir){
        File makedir=new File(dir);
        makedir.mkdir();
        System.out.println(makedir.isDirectory());
        System.out.println(makedir.getAbsolutePath());
        makedir.delete();
    }
</code></pre>
<h3 id="random-access-file">Random-Access File</h3>
<blockquote>
<p>Java provides the RandomAccessFile class to allow data to be read from and 
 written to at any locations in the file.</p>
</blockquote>
<p>the methods to access files are all <strong>sequential-access</strong> file</p>
<p><strong><code>RandomAccessFile</code> implements <code>Data(In|Out)put</code></strong></p>
<p>should specify one of the two modes: <strong>r</strong> or <strong>rw</strong></p>
<p><img alt="RAF" src="IO/photo/image-20200909152952005.png" /></p>
<pre><code class="language-java">public static void RandomAccessFileTest()throws IOException{
    RandomAccessFile raf=new RandomAccessFile(&quot;temp.txt&quot;,&quot;rw&quot;);
    System.out.println(raf.readByte());
    raf.seek(0);
    raf.writeChars(&quot;你好&quot;);
    raf.seek(0);
    System.out.println(raf.readChar());//if the text is consistent of ASCII use Byte instead of Char 
}
</code></pre>
<h2 id="text-io">Text I/O</h2>
<h3 id="write">write</h3>
<h4 id="printwriter">PrintWriter</h4>
<blockquote>
<p>can be used to create a file and write data to a text file</p>
</blockquote>
<pre><code class="language-java">PrintWriter output=new PrintWriter(&quot;temp.txt&quot;);
</code></pre>
<p><img alt="image-20200905004228491" src="IO/photo/image-20200905004228491.png" /></p>
<h4 id="filewriter">FileWriter</h4>
<blockquote>
<p>remember to close the writer</p>
</blockquote>
<pre><code class="language-java">public  void writeTest(String file){//file write
        try{
            FileWriter writer=new FileWriter(file);
            writer.write(&quot;HelloWorld&quot;);
            writer.close();
        }catch(IOException ex){
            ex.printStackTrace();
        }
    }
</code></pre>
<h4 id="bufferedwriter">BufferedWriter</h4>
<blockquote>
<p>depend on the FileWriter which can directly rectify the file</p>
</blockquote>
<pre><code class="language-java">public  void BufferTest(String file) throws IOException {
        BufferedWriter writer=new BufferedWriter(new FileWriter(file));
        writer.write(&quot;Heloo&quot;,0,3);
        writer.append('a');
        writer.flush();
    }
</code></pre>
<h3 id="read">Read</h3>
<h4 id="scanner">Scanner</h4>
<p><img alt="image-20200905004936506" src="IO/photo/image-20200905004936506.png" /></p>
<p><code>Scanner input = new Scanner(new File(filename));</code></p>
<h4 id="filereader">FileReader</h4>
<pre><code class="language-java">public  void readTest(String file){
        try{
            FileReader reader=new FileReader(file);
            char[] cstr=new char[100];
            reader.read(cstr,0,5);
            System.out.println(cstr);
            reader.close();
        }catch(IOException ex){
            ex.printStackTrace();
        }
    }
</code></pre>
<h4 id="bufferedread">BufferedRead</h4>
<h2 id="binary-io">Binary I/O</h2>
<blockquote>
<p><strong><em>The abstract InputStream is the root class for reading binary data, and the abstract 
OutputStream is the root class for writing binary data.</em></strong></p>
</blockquote>
<p><img alt="image-20200909012317531" src="IO/photo/image-20200909012317531.png" /></p>
<blockquote>
<p><code>java.io.InputStream</code></p>
</blockquote>
<pre><code class="language-java">+ read(): int

+ read(b:byte[]): int

+ read(b:byte[], off: int, len : int) : int

+ close():void

+ skip(n:long):long
</code></pre>
<blockquote>
<p><code>java.io.OutputStream</code></p>
</blockquote>
<pre><code class="language-java">+ write(int b): void

+ read(b:byte[]): void

+ read(b:byte[], off: int, len : int) : void

+ close():void

+ flush():void
</code></pre>
<h3 id="fileinputoutputstream">File(Input|Output)Stream</h3>
<blockquote>
<p><code>java.io.FileNotFoundException</code>  或者 <code>java.io.IOException</code></p>
<p>OutputStream 构造函数有 append 选项</p>
</blockquote>
<pre><code class="language-java">Object(Input|Output)Stream //which depends on the File(Input|Output)Stream as Initialize parameter
</code></pre>
<p>可以直接用 <code>File(Input|Output)Stream</code> 进行输出读取</p>
<blockquote>
<p><strong><code>java.io.(In|Out)putStream</code> implement the <code>AutoClosable</code> interface</strong></p>
</blockquote>
<pre><code class="language-java">try(){
    try (AutoCloseableObjecct app = new AutoCloseableObjecct()) {
              System.out.println(&quot;--执行main方法--&quot;);
          } catch (Exception e) {
              System.out.println(&quot;--exception--&quot;);
          } finally {
              System.out.println(&quot;--finally--&quot;);
         }
}
</code></pre>
<h3 id="filterinoutputstream">Filter(In|Out)putStream</h3>
<blockquote>
<p>Filter streams are streams that filter bytes for some purpose. The basic byte input stream provides a read method that can be used only for reading bytes. If you want to read <code>integers</code>, <code>doubles</code>, or <code>strings</code>, you need a filter class to wrap the byte input stream. Using a filter class enables you to read integers, doubles, and strings instead of bytes and characters. <code>FilterInputStream</code>and <code>FilterOutputStream</code> are the base classes for filtering data. When you need to process primitive numeric types, use <code>DataInputStream</code> and <code>DataOutputStream</code> to filter bytes.</p>
</blockquote>
<h3 id="datainoutputstream">Data(In|Out)putStream</h3>
<blockquote>
<p><code>DataInputStream</code> reads bytes from the stream and converts them into appropriate primitive-type values or strings. </p>
<p><code>DataOutputStream</code> converts primitive-type values or strings into bytes and outputs the bytes to the stream.</p>
<p><code>DataInputStream</code> extends <code>FilterInputStream</code> and implements the <code>DataInput</code>interface</p>
<p>DataOutputStream<code>extends</code>FilterOutputStream<code>and 
implements the</code>DataOutput` interface</p>
</blockquote>
<p><img alt="DataInputStream" src="IO/photo/image-20200909141822978.png" /></p>
<p><img alt="DataOutputStream" src="IO/photo/image-20200909141957771.png" /></p>
<pre><code class="language-java">DataInputStream dataInputStream=new DataInputStream(new FileInputStream(&quot;temp.txt&quot;));
System.out.println(dataInputStream.read());
</code></pre>
<p>read method may occur <strong>EOFException</strong> which show the end of file or some thing else</p>
<h3 id="bufferedinoutputstream">Buffered(In|Out)putStream</h3>
<blockquote>
<p><strong>BufferedInputStream/BufferedOutputStream</strong> can be used to speed up input and output by reducing the number of disk reads and writes. </p>
</blockquote>
<p>Load  -&gt;  memory  -&gt; disk</p>
<p><code>BufferedInputStream</code></p>
<ul>
<li>+ <code>BufferedInputStream(in : InputStream)</code></li>
<li>+ <code>BufferedInputStream(in : InputStream,bufferSize : int)</code></li>
</ul>
<p><code>BufferedOutputStream</code></p>
<ul>
<li>+ <code>BufferedOutputStream(out : OutputStream)</code></li>
<li>+ <code>BufferedOutputStream(out : OutputStream,bufferSize : int)</code></li>
</ul>
<p><img alt="Buffered" src="IO/photo/image-20200909143502300.png" /></p>
<h3 id="objectinoutputstream">Object(In|Out)putStream</h3>
<blockquote>
<p>similar to <code>Data(In|Out)putStream</code> classes but used for Object relevant operations</p>
<p><strong>have the ability to read a Object</strong></p>
<p>use the key word <code>transient</code> to avoid be serialized</p>
</blockquote>
<h2 id="reading-data-from-the-web">Reading Data from the Web</h2>
<blockquote>
<p>create a <strong>URL</strong> with <code>java.net.URL</code> firstly</p>
</blockquote>
<p><code>public URL(String spec) throws MalformedURLException</code></p>
<p><code>URL url = new URL("http://www.google.com/index.html");</code></p>
<blockquote>
<p>after <strong>URL</strong>  using the <code>openStream</code> method defined in the <strong>URL</strong> class to open an input stream which can be used by <strong>Scanner</strong></p>
</blockquote>
<p>​   </p>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="Lambda.html" class="btn btn-neutral float-right" title="Lambda">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="Collection.html" class="btn btn-neutral" title="Collection"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
  </div>

  Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" aria-label="versions">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
      
        <span><a href="Collection.html" style="color: #fcfcfc;">&laquo; Previous</a></span>
      
      
        <span style="margin-left: 15px"><a href="Lambda.html" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '.';</script>
    <script src="js/theme.js" defer></script>
      <script src="search/main.js" defer></script>
    <script defer>
        window.onload = function () {
            SphinxRtdTheme.Navigation.enable(true);
        };
    </script>

</body>
</html>
